package cn.iocoder.yudao.module.cook.controller.admin.cailiao;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cook.controller.admin.cailiao.vo.*;
import cn.iocoder.yudao.module.cook.dal.dataobject.cailiao.CailiaoDO;
import cn.iocoder.yudao.module.cook.service.cailiao.CailiaoService;

@Tag(name = "管理后台 - 食谱材料")
@RestController
@RequestMapping("/cook/cailiao")
@Validated
public class CailiaoController {

    @Resource
    private CailiaoService cailiaoService;

    @PostMapping("/create")
    @Operation(summary = "创建食谱材料")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:create')")
    public CommonResult<Long> createCailiao(@Valid @RequestBody CailiaoSaveReqVO createReqVO) {
        return success(cailiaoService.createCailiao(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食谱材料")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:update')")
    public CommonResult<Boolean> updateCailiao(@Valid @RequestBody CailiaoSaveReqVO updateReqVO) {
        cailiaoService.updateCailiao(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食谱材料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cook:cailiao:delete')")
    public CommonResult<Boolean> deleteCailiao(@RequestParam("id") Long id) {
        cailiaoService.deleteCailiao(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食谱材料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:query')")
    public CommonResult<CailiaoRespVO> getCailiao(@RequestParam("id") Long id) {
        CailiaoDO cailiao = cailiaoService.getCailiao(id);
        return success(BeanUtils.toBean(cailiao, CailiaoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食谱材料分页")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:query')")
    public CommonResult<PageResult<CailiaoRespVO>> getCailiaoPage(@Valid CailiaoPageReqVO pageReqVO) {
        PageResult<CailiaoDO> pageResult = cailiaoService.getCailiaoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CailiaoRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得食谱材料列表")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:query')")
    public CommonResult<List<CailiaoRespVO>> getCailiaoList(@Valid CailiaoListReqVO listReqVO) {
        List<CailiaoDO> list = cailiaoService.getCailiaoList(listReqVO);
        return success(BeanUtils.toBean(list, CailiaoRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得食谱材料列表")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:query')")
    public CommonResult<List<CailiaoRespVO>> getCailiaoSimpleList() {
        List<CailiaoDO> list = cailiaoService.getCailiaoList(
                new CailiaoListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus())
        );
        return success(BeanUtils.toBean(list, CailiaoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出食谱材料 Excel")
    @PreAuthorize("@ss.hasPermission('cook:cailiao:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCailiaoExcel(@Valid CailiaoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CailiaoDO> list = cailiaoService.getCailiaoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食谱材料.xls", "数据", CailiaoRespVO.class,
                        BeanUtils.toBean(list, CailiaoRespVO.class));
    }

}